package com.wawayaya.db.bookshelf;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.wawayaya.api.ApiResponseData;
import com.wawayaya.api.interf.ApiResponseJsonListener;
import com.wawayaya.api.remote.ApiConstans;
import com.wawayaya.api.remote.ApiWawayaya;
import com.wawayaya.bean.Advert;
import com.wawayaya.bean.BookShelf;
import com.wawayaya.bean.Books;
import com.wawayaya.bean.HomePage;
import com.wawayaya.bean.Notice;
import com.wawayaya.util.TLog;
import com.wawayaya.util.TimeZoneUtil;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
 * RequestThread
 * 进行网络请求
 * @author
 * @version 1.0
 * @created 2015-11-3
 */
public class RequestThread extends Thread implements ApiResponseJsonListener {
        public static  final   int    rquestData = 100;      //首次数据加载
        public static  final   int    endData = 200;

        public static  final   int    secondData = 110;      //第二次数据请求
        public static  final   int    secondEndData = 120;   //第二次数据请求结束

        public static  final   int    errorData = 400;


        private Context mContext;
        private Handler mHandler;
        private Handler cHandler;
        private DBThread saveDB;

    public DBThread getSaveDB() {
        return saveDB;
    }

    public RequestThread(Context context, Handler handler){
            this.mHandler = handler;
            this.mContext = context;
            saveDB = new DBThread();
            saveDB.start();
        }
        public Handler getcHandler() {
            return cHandler;
        }


    Notice versionNotice = null;
        @Override
        public void run() {

            Looper.prepare();
            cHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case  ApiConstans.CONFIG:
                            saveDB.setReqHandler(cHandler);
                            TLog.log(" start ...." + TimeZoneUtil.getCurentDate());
                            startRequest();
                            break;
                        case ApiConstans.BOOKSHELF:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.BOOKSHELF, null, RequestThread.this);
                            break;
                        case ApiConstans.BOOK:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.BOOK, null, RequestThread.this);
                            break;
                        case ApiConstans.ADVERT:
                            ApiWawayaya.shelfRequest(mContext,ApiConstans.ADVERT,null,RequestThread.this);
                            break;
                        case ApiConstans.HOMEPAGE:
                            ApiWawayaya.shelfRequest(mContext,ApiConstans.HOMEPAGE,null,RequestThread.this);



                        case ApiConstans.SUPPORT:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.SUPPORT, null, RequestThread.this);
                            break;
                        case ApiConstans.NOTICE:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.NOTICE, null, RequestThread.this);
                            break;

                        case  ApiConstans.AREA:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.AREA, null, RequestThread.this);
                            break;
                        case ApiConstans.MYBOOKSHELF:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.MYBOOKSHELF, null, RequestThread.this);
                            break;
                        case endData:
                            TLog.log(" all insert DB。。。。。");
                            Message message = mHandler.obtainMessage();
                            message.what = endData;
                            message.obj=versionNotice;
                            mHandler.sendMessage(message);
                            break;
                            /* 3.0.0 一期暂时不提供
                       case ApiConstans.BANNER:
                            ApiWawayaya.shelfRequest(mContext, ApiConstans.BANNER, null, RequestThread.this);
                            break;*/
                    };
                }
            };
            Looper.loop();
        }


        @Override
        public void success1(ApiResponseData result) {

            TLog.log(" net request complete。。。。。" + result.getRquestType());
            Message msg  = saveDB.getcHandler().obtainMessage();
            switch (result.getRquestType()){
                case ApiConstans.CONFIG:
                    msg.what = ApiConstans.CONFIG;
                    TLog.log("config net request complete。。。。。");
                    checkIsComplete();

                    break;
                case ApiConstans.BOOKSHELF:
                    msg.what = ApiConstans.BOOKSHELF;
                    checkIsComplete();
                    break;
                case ApiConstans.BOOK:
                    msg.what = ApiConstans.BOOK;
                    TLog.log("book net request complete。。。。。"+TimeZoneUtil.getCurentDate());
                    checkIsComplete();
                    break;
                case ApiConstans.ADVERT:
                    msg.what = ApiConstans.ADVERT;
                    TLog.log("ADVERT net request complete。。。。。");
                    checkIsComplete();
                    break;
                case ApiConstans.HOMEPAGE:
                    TLog.log("HOMEPAGE net request complete。。。。。");
                    msg.what = ApiConstans.HOMEPAGE;
                    checkIsComplete();
                    break;



                case ApiConstans.NOTICE:
                    msg.what = ApiConstans.NOTICE;
                    break;
                case ApiConstans.BANNER:
                    msg.what = ApiConstans.BANNER;
                    break;
                case ApiConstans.SUPPORT:
                    msg.what = ApiConstans.SUPPORT;
                    break;
                case ApiConstans.MYBOOKSHELF:
                    msg.what = ApiConstans.MYBOOKSHELF;
                    break;
                case ApiConstans.AREA:
                    msg.what = ApiConstans.AREA;
                    break;
            };
            msg.obj = result;
            saveDB.getcHandler().sendMessage(msg);
        }

        @Override
        public void success0(ApiResponseData result) {
            mHandler.sendEmptyMessage(errorData);
        }

        @Override
        public void error(ApiResponseData result) {
            mHandler.sendEmptyMessage(errorData);
        }

       private  int   firstCount = 5;
       private  void  startRequest(){
           ApiWawayaya.shelfRequest(mContext, ApiConstans.CONFIG, null, RequestThread.this);
           ApiWawayaya.shelfRequest(mContext, ApiConstans.BOOKSHELF, null, RequestThread.this);
           ApiWawayaya.shelfRequest(mContext, ApiConstans.BOOK, null, RequestThread.this);
           ApiWawayaya.shelfRequest(mContext,ApiConstans.ADVERT,null,RequestThread.this);
           ApiWawayaya.shelfRequest(mContext, ApiConstans.HOMEPAGE, null, RequestThread.this);
       }

      static int currentCount = 0;
      private  void  checkIsComplete(  ){
          currentCount++;
          TLog.log("...currentcount...."+currentCount);
          if(currentCount==firstCount){
              mHandler.sendEmptyMessage(endData);
          }
      }
}